#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Font.hpp>
#include <math.h>
#include <sstream>

int main(){

    /*Initialization of Window*/
    sf::ContextSettings settings;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 2;

    sf::RenderWindow window(sf::VideoMode(800, 600), "Battle Test v0.0.1", sf::Style::Default,settings);

    /*Texture Loading*/
    sf::Texture a,b;
    a.loadFromFile("bg.png");
    b.loadFromFile("wheel.png");
    sf::Sprite x,y;

    /*Text Setup*/
    sf::Text text("0");
    text.setCharacterSize(60);
    text.setStyle(sf::Text::Bold);
    text.setColor(sf::Color::Red);
    text.setOrigin(text.getGlobalBounds().width/2, text.getGlobalBounds().height/2);
    text.setPosition(800.f/2.f, 600.f/2.f);

    /*Texture Setup*/
    x.setTexture(a);
    x.setPosition(800.f/2.f, 600.f/2.f);
    x.setOrigin(x.getTextureRect().width/2, x.getTextureRect().height/2);

    y.setTexture(b);
    y.setPosition(800.f/2.f, 925);
    y.setOrigin(y.getTextureRect().width/2, y.getTextureRect().height/2);

    /*Mouse Setup*/
    window.setMouseCursorVisible(false);

    sf::Vector2i center(400,300);
    sf::Vector2i m;
    int s, wheelLocation;

    /*Main Loop*/
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))                             // Fuck, Did somethin happin?
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::LostFocus) // Helpful for Devel
                window.close();
        }

        window.setFramerateLimit(60);

        m = sf::Mouse::getPosition();                               // Set Mouse Change Checker
        sf::Mouse::setPosition(center, window);                     // Reset Mouse to center
        s = m.x - sf::Mouse::getPosition().x;                       // Get diff in x
        y.setRotation(y.getRotation() + s);                         // Add to Rotation
        std::stringstream str;
        if(y.getRotation() < 0){                                    // If backwards, make sure stays positive
            y.setRotation(y.getRotation()+360);
        }
        wheelLocation = ((int)(ceil(y.getRotation() - 15)) % 360 / 30 + 1);
                                                                    // Fancy Math divides the wheel into 12,
                                                                    // gets location
        str << wheelLocation;
        text.setString(str.str());                                  // Set Debug Text

        window.draw(x);                                             // Draw BG
        window.draw(y);                                             // Draw Wheel
        window.draw(text);                                          // Draw Location
        window.display();

    }

    return 0;
}
